package cqrtplm.controller;

import com.ty.basic.response.ResponseResult;
import com.hustcad.plm.pdm.middle.partbom.service.TyplmPdmPartService;
import com.hustcad.plm.pdm.partbom.service.TyplmPartService;
import com.ty.basic.constant.ApiPathConstant;
import com.ty.basic.entity.part.PartDO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping(ApiPathConstant.PRODUCT_PDM_PATH + "/Order/")
@Api(tags = "查询下单锁定")
public class OrderLockController {
    @Resource
    private TyplmPartService typlmPartService;
    @Resource
    private TyplmPdmPartService typlmPdmPartService;

    /**
     * 根据零件编号查询零件生命周期状态，并判断是否处于锁定状态
     *
     * @param partNumber 零件编号
     * @return ResponseResult 响应结果，包含判断结果(未锁定/锁定)
     */
    @PostMapping("/Lock")
    @ApiOperation("根据编码查询物料状态")
    public ResponseResult lockOrder(String partNumber) {
        try {
            PartDO partDO = typlmPdmPartService.queryLastPartDoByPartNumber(partNumber);
            if (partDO == null) {
                throw new IllegalArgumentException("未找到此成品码");
            }
            String lifecycleState = partDO.getLifecyclestagekey();
            List<String> lockstatus = Arrays.asList("RELEASED");
            return ResponseResult.success(lockstatus.contains(lifecycleState) ? "未锁定" : "锁定");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.fail("400", e.getMessage());
        }

    }
}
